package com.homework.controller;

import com.alibaba.fastjson.JSON;
import com.homework.entity.ResponseResult;
import com.homework.entity.Student;
import com.homework.service.StudentService;
import com.homework.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: XiangHu
 * @date: 2025年03月02日 23:43
 * @describe:
 */
@WebServlet("/updatestudent")
public class UpdateStudent extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");

        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        int age = Integer.parseInt(req.getParameter("age"));

        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setAge(age);

        StudentService studentService = new StudentServiceImpl();
        int result = studentService.updateStudent(student);
        resp.getWriter().write(JSON.toJSONString(new ResponseResult(200, "OK", student)));
    }
}